<!--
# CIS 2515 FINAL PROJECT
# Author: Malacarne, Nicholas SID: 7770
# Description: This script handles Glyph combinations as well as generating the
# Glyph combining page.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
include('/home/vhosts/cfentertainment.freevar.com/Lib/PlayerDef.php');
include('/home/vhosts/cfentertainment.freevar.com/Lib/GlyphDef.php');
include('/home/vhosts/cfentertainment.freevar.com/Lib/SuperHTMLDef.php');
include('/home/vhosts/cfentertainment.freevar.com/Lib/DBMgrDef.php');
?>
<?php session_start(); ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Glyph</title>
    </head>
    <body>
<?php


// How many times the script has been run.
$step = $_REQUEST["step"] + 1;

// What action needs to be taken by the script.
$action = $_REQUEST["action"];

// What card did the user choose to combine.
$card = $_REQUEST["card"];

$players = $_SESSION["players"];

if ($action == null)
{
 /* Sets the step to zero. We do this because the user has not
  * selected any Glyphs to combine, so we need to display the page
  * accordingly.
  */$step = 0;

    $page = buildPage($players, $step);
    echo $page;
}
else if ($action == "select")
{
    /* Removes the selected card from the player's hand and stores it in
     * a session variable. i.e. step 1 = "card1"
     */$_SESSION["card".$step] = grabCard($players, $card);

       $_SESSION["players"] = $players;
       $page = buildPage($players, $step);
       echo $page;
}
else if ($action == "combine")
{
 /* Stores the selected cards into an array.
  */$cards = array($_SESSION["card1"], $_SESSION["card2"]);
  
 /* Combines both cards and creates a new card. If there is no entry
  * in the database that matches the new card, then the script dies.
  */$newCard = combineCards($players, $cards) or die("NOT FOUND IN DATABASE!");

    $_SESSION["players"] = $players;
    $page = buildPage($players, $step, $newCard);
    echo $page;
}

/***************************************************************************** *
* Function Name: combineCards
* Author: Nicholas Malacarne
* Parameters: $players4 - Array of PlayerDef obj.
 *            $cards4 - An array which holds the cards to combine.
* Description: Removes the selected card from the player's hand.
***************************************************************************** */
function combineCards($players4, $cards4)
{
    $wind = 0;
    $fire = 0;
    $water = 0;
    $earth = 0;
    $death = 0;
    $life = 0;

 /* Determines what each card is made of. i.e. 'Lightning' contains 'Fire'
  * plus 'Air'. Once we find a component, we add one to the corresponding
  * variable.
  */foreach ($cards4 as $card)
    {
        if ($card->isFire())
        {
            $fire += 1;
        }
        if ($card->isWind())
        {
            $wind += 1;
        }
        if ($card->isWater())
        {
            $water += 1;
        }
        if ($card->isEarth())
        {
            $earth += 1;
        }
        if ($card->isDeath())
        {
            $death += 1;
        }
        if ($card->isLife())
        {
            $life += 1;
        }
    }

 /* Here we check to see what we need to create according to the elements
  * of the cards the player chose to combine.
  */if ($fire == 1 && $wind == 1)
    {
        $newCard = new GlyphDef("Lightning");
    }
    else if ($fire == 1 && $earth == 1)
    {
        $newCard = new GlyphDef("Ash");
    }
    else if ($death == 1 && $earth == 1)
    {
        $newCard = new GlyphDef("Decay");
    }
    else if ($water == 1 && $death == 1)
    {
        $newCard = new GlyphDef("Poison");
    }
    else if ($death == 1 && $life == 1)
    {
        $newCard = new GlyphDef("Draining");
    }
    else if ($life == 2)
    {
        $newCard = new GlyphDef("Vigor");
    }
    else if ($death == 2)
    {
        $newCard = new GlyphDef("Demise");
    }
    else if ($fire == 2)
    {
        $newCard = new GlyphDef("Inferno");
    }
    else if ($water == 2)
    {
        $newCard = new GlyphDef("Current");
    }


 /* If we've created a new obj., then add that obj. to the player's hand and
  * return it's name to the function call. If we didn't find an entry in the
  * db for the card we wanted, we return false.
  */if (is_object($newCard))
    {
        foreach ($players4 as $player)
        {
            if ($player->isTurn())
            {
                $player->addHand($newCard);
                $player->setInk(-$newCard->getTier() - 1);
            }
        }
        
        return $newCard->getName();
    }
    else
    {
        return false;
    }
}

/***************************************************************************** *
* Function Name: grabCard
* Author: Nicholas Malacarne
* Parameters: $players1 - Array of PlayerDef obj.
 *            $curKey1 - The card's element location.
* Description: Removes the selected card from the player's hand.
***************************************************************************** */
function grabCard($players1, $curKey1)
{
    foreach ($players1 as $player)
    {
        if ($player->isTurn())
        {
         /* Copies the correct cards.
          */foreach ($player->getHand() as $key => $card)
            {
             /* We've found the card we want when the array's element matches
              * the number of the elements the user chose.
              */if ($key == $curKey1)
                {
                 /* Copies the element into a new array.
                  */$theCard = array_slice($player->getHand(), $key, 1);

                 /* Removes the card from the player's hand.
                  */$player->setHand($key);
                }
            }
        }
    }

 /* Returns the chosen card to the function call.
  */return $theCard[0];
}

/***************************************************************************** *
* Function Name: buildPage
* Author: Nicholas Malacarne
* Parameters: $players0 - Array of PlayerDef obj.
 *            $step0 - What step the script is currently on.
 *            $outcome0 - The name of the created card.
* Description: Builds the combine page.
***************************************************************************** */
function buildPage($players0, $step0, $outcome0)
{
    $HTML = new SuperHTMLDef("Glyph");

 /* Build the banner area of the page.
  */$HTML->buildTop();
    $HTML->addHTML("<center>");
    $HTML->h1("GLYPH");

 /* At step zero, no Glyphs are chosen. At step 1, the player has chosen the
  * first Glyph. At step 2, the second Glyph has been chosen.
  */if ($step0 == 0)
    {
        $HTML->addText(addHand($players0, $step0));
    }
    else if ($step0 == 1)
    {
        $HTML->addHTML("Glyph of ".$_SESSION["card1"]->getName()." + ");
        $HTML->addText(addHand($players0, $step0));
    }
    else if ($step0 == 2)
    {
        $HTML->addHTML("Glyph of ".$_SESSION["card1"]->getName()." + ".
                       "Glyph of ".$_SESSION["card2"]->getName());
        $HTML->addHTML("<br>");
        $HTML->addHTML("<br>");
        $HTML->addHTML("<form method=\"post\" action=\"combine.php\">");
        $HTML->submit("Combine!");
        $HTML->textbox("hidden", "action", "combine");
        $HTML->textbox("hidden", "step", $step0);
        $HTML->addHTML("</form>");
        $HTML->addHTML("<form method=\"post\" action=\"battle.php\">");
        $HTML->addText("<input type=\"image\"
                       src = \"http://cfentertainment.freevar.com/images/Glyph/back.jpg\">\n");
        $HTML->textbox("hidden", "page", "combBack");
        $HTML->addHTML("</form>");
    }
    else if ($step0 > 2)
    {
        $HTML->addHTML("Created: Glyph of ".$outcome0."!");
        $HTML->addHTML("<form method=\"post\" action=\"battle.php\">");
        $HTML->submit("Done");
        $HTML->textbox("hidden", "page", "combined");
        $HTML->textbox("hidden", "newCard", $outcome0);
        $HTML->addHTML("</form>");
    }

    $HTML->addHTML("</center>");
    $HTML->buildBottom();

    return $HTML->getPage();
}

/***************************************************************************** *
* Function Name: addHand
* Author: Nicholas Malacarne
* Parameters: $players2 - Array of PlayerDef obj.
 *            $step2 - What step the script is currently on.
* Description: Stores data in database.
***************************************************************************** */
function addHand($players2, $step2)
{
    $HTML = new SuperHTMLDef();

    $HTML->addHTML("<form method=\"post\" action=\"combine.php\">");
    $HTML->startTable();
    $HTML->addHTML("<tr>");

    foreach ($players2 as $player)
    {
        if ($player->isTurn())
        {
            $HTML->addHTML("<td align=\"center\">");
            $HTML->startSelect("card");

            foreach ($player->getHand() as $key => $card)
            {
                if($player->getInk() >= $card->getTier())
                {
                 /* Formats the card's name and tier value.
                  */$formatCard = $card->getName()."(T".$card->getTier().")";

                 /* Adds the formatted text as an option in the select box.
                  */$HTML->addOption($key, $formatCard);
                }
            }

            $HTML->endSelect();
            $HTML->addHTML("</td>");
            $HTML->addHTML("<th colspan=\"2\">");
            $HTML->addText("<input type=\"image\"
                           src = \"http://cfentertainment.freevar.com/images/Glyph/select.jpg\">\n");
            $HTML->textbox("hidden", "action", "select");
            $HTML->textbox("hidden", "step", $step2);
            $HTML->addHTML("</th>");
        }
    }

    $HTML->addHTML("</tr>");
    $HTML->addHTML("</table>");
    $HTML->addHTML("</form>");

    return $HTML->getPage();
}
?>
    </body>
</html>